//
//  ActivityDetailViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "AppTools.h"
#import "Activity.h"
#import "ActivitySingleton.h"
#import "LoginViewController.h"
@interface ActivityDetailViewController ()

@end

@implementation ActivityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.648 green:1.000 blue:0.979 alpha:1.000];
    self.navigationItem.title = self.activiyX.title;
    
    self.activityDetaView = [[ActivityDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.activityDetaView.backgroundColor = [UIColor whiteColor];
    self.activityDetaView.Bactivity = self.activiyX;
    [self.view addSubview:self.activityDetaView];
    [_activityDetaView release];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
    // 改变颜色(不好用)
//    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    

    
}

- (void)leftAction:(UIBarButtonItem *)barbtn1
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



-(void)rightAction:(UIBarButtonItem *)barbtn2
{
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
 
    if (name.length == 0) {
        UIAlertView *alerVier = [[UIAlertView alloc] initWithTitle:@"还未登陆不能进行收藏" message:@"请进行登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alerVier show];
        // 模态出登陆
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }else{
        [[ActivitySingleton shareActivitySingleton] openAB];
        [[ActivitySingleton shareActivitySingleton] createTable];
        NSMutableArray *array = [NSMutableArray array];
        array = [[ActivitySingleton shareActivitySingleton] selectActivity:name];
        
        if (array.count != 0) {
            for (Activity *act in array) {
                if ([act.title isEqualToString:self.activityDetaView.titleLable.text]) {
                    
                    UIAlertView *alerVier = [[UIAlertView alloc] initWithTitle:@"已经收藏过该电影" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    [alerVier show];
                    return;
                    
                }
            
            }
            {
                [[ActivitySingleton shareActivitySingleton] insertActivity:self.activiyX UserName:name];
                UIAlertView *alerVier = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alerVier show];
            }
            
            
        }else{
            
            [[ActivitySingleton shareActivitySingleton] insertActivity:self.activiyX UserName:name];
            UIAlertView *alerVier = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alerVier show];
            
        }
      
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
